/* global echarts */
app.controller('DashBoradCtrl', function ($scope, ajax, $filter, toaster, $rootScope) {

	$scope.flag = false;
	$scope.starList = [];
	$scope.orgList = [];

	$scope.$watch('stats.booking', function (newVal, oldVal) {	// jshint unused:false
		if (!newVal) {
			$scope.starList = [];
		} else {
			//起始客运站列表读取
			ajax.get('starting/bk/list', function (result) {
				if (result && result.code == 200) $scope.starList = result.result;
			}, function () { });
		}
	});

	$scope.$watch('stats.bus', function (newVal, oldVal) {	// jshint unused:false
		if (!newVal) {
			$scope.orgList = [];
		} else {
			//运营单位列表读取
			ajax.get('organization/3/list', function (result) {
				if (result && result.code == 200) $scope.orgList = result.result;
			}, function () { });
		}
	});

	if ($rootScope.loginUser && $rootScope.loginUser.roles) {
		for (var i = 0; i < $rootScope.loginUser.roles.length; i++) {
			if ($rootScope.loginUser.roles[i] == "ROLE_ADMIN") {
				$scope.flag = true;
			}
		}
	}

	$scope.getDateStr = function (addDayCount) {
		var dd = new Date();
		dd.setDate(dd.getDate() + addDayCount);
		var y = dd.getFullYear();
		var m = dd.getMonth() + 1;
		var d = dd.getDate();
		return y + "-" + m + "-" + d + " 00:00:00";
	};

	$scope.maxSize = function (dom) {
		dom._dom.style.width = window.innerWidth - 270 + 'px';
		dom._dom.style.height = window.innerHeight - 270 + 'px';
		dom.resize();
	};

	$scope.charts = [];
	window.onresize = function () {
		for (var i = 0; i < $scope.charts.length; i++) {
			$scope.maxSize($scope.charts[i]);
		}
	};

});

app.controller('OrderCountCtrl', function ($scope, ajax, $filter, toaster) {

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.setOption = function (list) {
		echarts.init(document.getElementById('orderCount'));
		var count = list[4] + list[5] + list[6];
		var option = {
			title: {
				text: '订单数据统计',
				subtext: '此处图表为每小时各类订单新增的数量，当前图表新增总订单数量为：' + count + '单',
				x: 'center',
				align: 'right'
			},
			tooltip: {
				trigger: 'axis'
			},
			legend: {
				left: 'left',
				data: ['订票订单', '城际约租订单', '跨城巴士订单']
			},
			xAxis: {
				data: list[0],
				splitLine: {
					show: true
				}
			},
			yAxis: {
				splitLine: {
					show: true
				}
			},
			toolbox: {
				feature: {
					dataView: { show: true, readOnly: false },
					magicType: { show: true, type: ['line', 'bar'] },
					restore: { show: true },
					saveAsImage: { show: true }
				}
			},
			dataZoom: [{
				startValue: 0,
				endValue: 100
			}, {
				type: 'inside'
			}],
			series: [
				{
					name: '订票订单',
					type: 'line',
					data: list[1]
				},
				{
					name: '城际约租订单',
					type: 'line',
					data: list[2]
				},
				{
					name: '跨城巴士订单',
					type: 'line',
					data: list[3]
				},
				{
					name: '订单占比',
					type: 'pie',
					radius: '25%',
					center: ['82.5%', '23.5%'],
					data: [
						{ value: list[4], name: '订票订单' },
						{ value: list[5], name: '城际约租订单' },
						{ value: list[6], name: '跨城巴士订单' },

					],
					itemStyle: {
						emphasis: {
							shadowBlur: 10,
							shadowOffsetX: 0,
							shadowColor: 'rgba(0, 0, 0, 0.5)'
						}
					},
					tooltip: {
						trigger: 'item',
						formatter: "{a} <br/>{b} : {c} ({d}%)"
					}
				}
			]
		};

		var dom = document.getElementById("orderCount");
		var orderCount = echarts.init(dom);
		orderCount.setOption(option, true);
		$scope.charts.push(orderCount);
		$scope.maxSize(orderCount);
	};

	$scope.timeRange = {};
	if ($scope.flag) {
		var start = $scope.getDateStr(0);
		var end = $scope.getDateStr(1);
		$scope.timeRange.startTime = $filter("date")(start, "yyyy-MM-dd HH:mm:ss");
		$scope.timeRange.endTime = $filter("date")(end, "yyyy-MM-dd HH:mm:ss");
		inserter.fields = $scope.timeRange;
		inserter.resource = 'count/orderDashboard';
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				if (result.result != null) {
					$scope.setOption(result.result);
				}
			} else {
				toaster.pop('error', '错误', result.message);
			}
		}, function () {
				toaster.pop('error', '错误', '与服务器的连接失败！');
		});

		delete $scope.timeRange.startTime;
		delete $scope.timeRange.endTime;
	}

	$scope.query = function () {
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			if ($scope.timeRange.startTime >= $scope.timeRange.endTime) {
				toaster.pop('info', '提示', '起始时间必须小于结束时间！');
				return;
			}
			$scope.timeRange.startTime = $filter("date")($scope.timeRange.startTime, "yyyy-MM-dd HH:mm:ss");
			$scope.timeRange.endTime = $filter("date")($scope.timeRange.endTime, "yyyy-MM-dd HH:mm:ss");
			inserter.fields = $scope.timeRange;
			inserter.resource = 'count/orderDashboard';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					if (result.result[0]) $scope.setOption(result.result);
				} else {
					toaster.pop('error', '错误', result.message);
				}
			}, function () {
				toaster.pop('error', '错误', '与服务器的连接失败！');
			});
		} else {
			toaster.pop('info', '提示', '请选择时间段！');
		}
	};
});

app.controller('PassengerCountCtrl', function ($scope, ajax, $filter, toaster) {
	var inserter = new Req.POST({ resource: $scope.resource });
	$scope.setOption = function (list) {
		var option = {
			title: {
				text: '乘客用户数量统计',
				subtext: '此处图表为每小时新增乘客用户数量，当前图表新增乘客用户总量为：' + list[2] + '个',
				x: 'center',
				align: 'right'
			},
			tooltip: {
				trigger: 'axis'
			},
			toolbox: {
				feature: {
					dataView: { show: true, readOnly: false },
					magicType: { show: true, type: ['line', 'bar'] },
					restore: { show: true },
					saveAsImage: { show: true }
				}
			},
			dataZoom: [{
				startValue: 0,
				endValue: 100
			}, {
				type: 'inside'
			}],
			xAxis: [
				{
					data: list[0],
					splitLine: {
						show: true
					}
				}
			],
			yAxis: {
				splitLine: {
					show: true
				}
			},
			series: [
				{
					name: '新增乘客用户',
					type: 'bar',
					data: list[1]
				},
			]
		};

		var dom = document.getElementById("countPas");
		var countPas = echarts.init(dom);
		countPas.setOption(option, true);
		$scope.charts.push(countPas);
		$scope.maxSize(countPas);
	};

	$scope.timeRange = {};
	if ($scope.flag) {
		var start = $scope.getDateStr(0);
		var end = $scope.getDateStr(1);
		$scope.timeRange.startTime = $filter("date")(start, "yyyy-MM-dd HH:mm:ss");
		$scope.timeRange.endTime = $filter("date")(end, "yyyy-MM-dd HH:mm:ss");
		inserter.fields = $scope.timeRange;
		inserter.resource = 'count/passengerDashborad';
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				if (result.result != null) {
					$scope.setOption(result.result);
				}
			} else {
				toaster.pop('error', '错误', result.message);
			}
		}, function () {
			toaster.pop('error', '错误', '与服务器的连接失败！');
		});

		delete $scope.timeRange.startTime;
		delete $scope.timeRange.endTime;
	}

	$scope.query = function () {
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			if ($scope.timeRange.startTime >= $scope.timeRange.endTime) {
				toaster.pop('info', '提示', '起始时间必须小于结束时间！');
				return;
			}
			$scope.timeRange.startTime = $filter("date")($scope.timeRange.startTime, "yyyy-MM-dd HH:mm:ss");
			$scope.timeRange.endTime = $filter("date")($scope.timeRange.endTime, "yyyy-MM-dd HH:mm:ss");
			inserter.fields = $scope.timeRange;
			inserter.resource = 'count/passengerDashborad';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					if (result.result[0] != null) {
						$scope.setOption(result.result);
					}
				} else {
					toaster.pop('error', '错误', result.message);
				}
			}, function () {
				toaster.pop('error', '错误', '与服务器的连接失败！');
			});
		} else {
			toaster.pop('info', '提示', '请选择时间段！');
		}
	};
});

app.controller('SMSCtrl', function ($scope, ajax, $filter, toaster) {

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.setOption = function (list) {
		var option = {
			title: {
				text: '短信发送次数统计',
				subtext: '此处图表为每小时短信发送成功和失败的数量，当前图表短信发送总次数为：' + list[3] + '次',
				x: 'center',
				align: 'right'
			},
			tooltip: {
				trigger: 'axis'
			},
			toolbox: {
				feature: {
					dataView: { show: true, readOnly: false },
					magicType: { show: true, type: ['line', 'bar'] },
					restore: { show: true },
					saveAsImage: { show: true }
				}
			},
			dataZoom: [{
				startValue: 0,
				endValue: 100
			}, {
				type: 'inside'
			}],
			legend: {
				x: 'left',
				data: ['发送短信成功次数', '发送短信失败次数']
			},
			xAxis: [
				{
					data: list[0],
					splitLine: {
						show: true
					}
				}
			],
			yAxis: {
				splitLine: {
					show: true
				}
			},
			series: [
				{
					name: '发送短信成功次数',
					type: 'line',
					data: list[1]
				},
				{
					name: '发送短信失败次数',
					type: 'line',
					data: list[2]
				},
			]
		};

		var dom = document.getElementById("smsCount");
		var smsCount = echarts.init(dom);
		smsCount.setOption(option, true);
		$scope.charts.push(smsCount);
		$scope.maxSize(smsCount);
	};

	$scope.timeRange = {};
	if ($scope.flag) {
		var start = $scope.getDateStr(0);
		var end = $scope.getDateStr(1);
		$scope.timeRange.startTime = $filter("date")(start, "yyyy-MM-dd HH:mm:ss");
		$scope.timeRange.endTime = $filter("date")(end, "yyyy-MM-dd HH:mm:ss");
		inserter.fields = $scope.timeRange;
		inserter.resource = 'count/smsrDashborad';
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				if (result.result != null) {
					$scope.setOption(result.result);
				}
			} else {
				toaster.pop('error', '错误', result.message);
			}
		}, function () {
			toaster.pop('error', '错误', '与服务器的连接失败！');
		});

		delete $scope.timeRange.startTime;
		delete $scope.timeRange.endTime;
	}

	$scope.query = function () {
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			if ($scope.timeRange.startTime >= $scope.timeRange.endTime) {
				toaster.pop('info', '提示', '起始时间必须小于结束时间！');
				return;
			}
			$scope.timeRange.startTime = $filter("date")($scope.timeRange.startTime, "yyyy-MM-dd HH:mm:ss");
			$scope.timeRange.endTime = $filter("date")($scope.timeRange.endTime, "yyyy-MM-dd HH:mm:ss");
			inserter.fields = $scope.timeRange;
			inserter.resource = 'count/smsrDashborad';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					if (result.result[0] != null) {
						$scope.setOption(result.result);
					}
				} else {
					toaster.pop('error', '错误', result.message);
				}
			}, function () {
				toaster.pop('error', '错误', '与服务器的连接失败！');
			});
		} else {
			toaster.pop('info', '提示', '请选择时间段！');
		}
	};

});

app.controller('BookingCtrl', function ($scope, ajax, $filter, toaster) {

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.queryMap = {};

	$scope.setOption = function (list) {
		$scope.ticketList = list[0];
		$scope.number = 0;
		for (var i = 0; i < $scope.ticketList.length; i++) {
			$scope.number += $scope.ticketList[i];
		}
		var option = {
			title: {
				text: '订票模块班次售票统计',
				subtext: '此处图表为订票模块每个班次时段售票统计，当前图表总售票：' + $scope.number + '张。（统计的数据中不包含订单超时的订单）',
				x: 'center',
				align: 'right'
			},
			tooltip: {
				trigger: 'axis'
			},
			toolbox: {
				feature: {
					dataView: { show: true, readOnly: false },
					magicType: { show: true, type: ['line', 'bar'] },
					restore: { show: true },
					saveAsImage: { show: true }
				}
			},
			dataZoom: [{
				startValue: 0,
				endValue: 100
			}, {
				type: 'inside'
			}],
			xAxis: [
				{
					data: list[1],
					splitLine: {
						show: true
					}
				}
			],
			yAxis: {
				splitLine: {
					show: true
				}
			},
			series: [
				{
					name: '已售车票',
					type: 'line',
					data: list[0]
				},
			]
		};

		var dom = document.getElementById("bookingCount");
		var bookingCount = echarts.init(dom);
		bookingCount.setOption(option, true);
		$scope.charts.push(bookingCount);
		$scope.maxSize(bookingCount);
	};

	$scope.timeRange = {};
	if ($scope.flag) {
		var start = $scope.getDateStr(0);
		var end = $scope.getDateStr(1);
		$scope.queryMap.startTime = $filter("date")(start, "yyyy-MM-dd HH:mm:ss");
		$scope.queryMap.endTime = $filter("date")(end, "yyyy-MM-dd HH:mm:ss");
		$scope.queryMap.startId = 0;
		inserter.fields = $scope.queryMap;
		inserter.resource = 'count/bookingDashborad';
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				if (result.result != null) {
					$scope.setOption(result.result);
				}
			} else {
				toaster.pop('error', '错误', result.message);
			}
		}, function () {
			toaster.pop('error', '错误', '与服务器的连接失败！');
		});

		delete $scope.queryMap.startTime;
		delete $scope.queryMap.endTime;
	}

	$scope.query = function () {
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			if ($scope.timeRange.startTime >= $scope.timeRange.endTime) {
				toaster.pop('info', '提示', '起始时间必须小于结束时间！');
				return;
			}
			$scope.queryMap.startTime = $filter("date")($scope.timeRange.startTime, "yyyy-MM-dd HH:mm:ss");
			$scope.queryMap.endTime = $filter("date")($scope.timeRange.endTime, "yyyy-MM-dd HH:mm:ss");
			$scope.queryMap.startId = 0;
			if ($scope.starList.selected != undefined) {
				$scope.queryMap.startId = $scope.starList.selected.id;
			}
			inserter.fields = $scope.queryMap;
			inserter.resource = 'count/bookingDashborad';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					if (result.result[0] != null) {
						$scope.setOption(result.result);
					}
				} else {
					toaster.pop('error', '错误', result.message);
				}
			}, function () {
				toaster.pop('error', '错误', '与服务器的连接失败！');
			});
		} else {
			toaster.pop('info', '提示', '请选择时间段！');
		}
	};

});

app.controller('BusCtrl', function ($scope, ajax, $filter, toaster) {

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.queryMap = {};

	$scope.setOption = function (list) {
		$scope.ticketList = list[0];
		$scope.number = 0;
		for (var i = 0; i < $scope.ticketList.length; i++) {
			$scope.number += $scope.ticketList[i];
		}
		var option = {
			title: {
				text: '跨城巴士班次售票统计',
				subtext: '此处图表为跨乘巴士每个班次时段售票统计，当前图表总售票：' + $scope.number + '张。（统计的数据中不包含订单超时的订单）',
				x: 'center',
				align: 'right'
			},
			tooltip: {
				trigger: 'axis'
			},
			toolbox: {
				feature: {
					dataView: { show: true, readOnly: false },
					magicType: { show: true, type: ['line', 'bar'] },
					restore: { show: true },
					saveAsImage: { show: true }
				}
			},
			dataZoom: [{
				startValue: 0,
				endValue: 100
			}, {
				type: 'inside'
			}],
			xAxis: [
				{
					data: list[1],
					splitLine: {
						show: true
					}
				}
			],
			yAxis: {
				splitLine: {
					show: true
				}
			},
			series: [
				{
					name: '已售车票',
					type: 'line',
					data: list[0]
				},
			]
		};

		var dom = document.getElementById("busCount");
		var busCount = echarts.init(dom);
		busCount.setOption(option, true);
		$scope.charts.push(busCount);
		$scope.maxSize(busCount);
	};

	$scope.timeRange = {};
	if ($scope.flag) {
		var start = $scope.getDateStr(0);
		var end = $scope.getDateStr(1);
		$scope.queryMap.startTime = $filter("date")(start, "yyyy-MM-dd HH:mm:ss");
		$scope.queryMap.endTime = $filter("date")(end, "yyyy-MM-dd HH:mm:ss");
		$scope.queryMap.orgId = 0;
		inserter.fields = $scope.queryMap;
		inserter.resource = 'count/busDashborad';
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				if (result.result != null) {
					$scope.setOption(result.result);
				}
			} else {
				toaster.pop('error', '错误', result.message);
			}
		}, function () {
			toaster.pop('error', '错误', '与服务器的连接失败！');
		});

		delete $scope.queryMap.startTime;
		delete $scope.queryMap.endTime;
	}

	$scope.query = function () {
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			if ($scope.timeRange.startTime >= $scope.timeRange.endTime) {
				toaster.pop('info', '提示', '起始时间必须小于结束时间！');
				return;
			}
			$scope.queryMap.startTime = $filter("date")($scope.timeRange.startTime, "yyyy-MM-dd HH:mm:ss");
			$scope.queryMap.endTime = $filter("date")($scope.timeRange.endTime, "yyyy-MM-dd HH:mm:ss");
			$scope.queryMap.orgId = 0;
			if ($scope.orgList.selected != undefined) {
				$scope.queryMap.orgId = $scope.orgList.selected.id;
			}
			inserter.fields = $scope.queryMap;
			inserter.resource = 'count/busDashborad';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					if (result.result[0] != null) {
						$scope.setOption(result.result);
					}
				} else {
					toaster.pop('error', '错误', result.message);
				}
			}, function () {
				toaster.pop('error', '错误', '与服务器的连接失败！');
			});
		} else {
			toaster.pop('info', '提示', '请选择时间段！');
		}
	};

});
